const Router = require('koa-router');
const router = new Router();

const emailLogic = require('../business-logic/email');

router.get('/emails', async(ctx, next) => {
    let query = ctx.request.query;
    let emails = await emailLogic.query(query.pageNum, query.pageCount, {
        state: 1
    });
    let total = await emailLogic.total();
    ctx.send({
        code: 200,
        data: {
            emails: emails,
            total: total
        }
    })
})

router.get('/all-emails', async(ctx, next) => {
    let emails = await emailLogic.queryAll();
    ctx.send({
        code: 200,
        data: emails
    })
})

router.post('/email', async(ctx, next) => {
    let body = ctx.request.body;
    await emailLogic.save(body);
    ctx.send({
        code: 200,
        msg: 'save email success'
    });
})

router.delete('/email/:id', async(ctx, next) => {
    let id = ctx.params.id;
    await emailLogic.remove(id);
    ctx.send({
        code: 200,
        msg: 'remove email success'
    });
})

module.exports = router;